#include "videoOption.h"
#include "HiAutoIncrease.h"
#include <stdio.h>

static Hi::HiAutoIncrease ssrc_proxy(54673);

static const char *FFMPEG_FMT =
    "ffmpeg -v info -re  -stream_loop -1 -probesize 409600 -fpsprobesize 1  "
    "-fflags nobuffer -i %s  -y -c:v copy  -bsf:v h264_mp4toannexb,dump_extra "
    "-map 0:v:0 "
    " -f  tee [select=v:f=rtp:ssrc=%ld:payload_type=100]rtp://127.0.0.1:%d > "
    "ffmpeg_%d.log  2>&1";

static const char *FFMPEG_PART_FMT =
    "f=rtp:ssrc=%ld:payload_type=100]rtp://127.0.0.1:%d";

static const char *SHARE_PATH_FMT = "/tmp/hi/video_test_%ld";

void VideoOption::init(const char *rtsp, uint16_t port, uint32_t width,
                       uint32_t height, long flag) {
    printf("\nVideoOption input\n\t"
           "rtsp_url:%s\n\t"
           "port:%d\n\t"
           "width:%d\n\t"
           "height:%d\n\t"
           "flag:%ld\n\n",
           rtsp, port, width, height, flag);

    this->width = width;
    this->height = height;
    this->port = port;

    long ssrc = ssrc_proxy.get();

    char ffmpeg[2048] = {0};
    snprintf(ffmpeg, sizeof(ffmpeg), FFMPEG_FMT, rtsp, ssrc, port, port);
    this->ffmpeg_cmd = ffmpeg;

    printf("ffmpeg cmd:%s\n", ffmpeg);

    char ffmpeg_part[128] = {0};
    snprintf(ffmpeg_part, sizeof(ffmpeg_part), FFMPEG_PART_FMT, ssrc, port);
    this->ffmpeg_cmd_part = ffmpeg_part;

    system("mkdir /tmp/hi");

    char share_path[128] = {0};
    snprintf(share_path, sizeof(share_path), SHARE_PATH_FMT, flag);
    this->share_path = share_path;
}

const char *VideoOption::get_ffmepg_cmd() { return this->ffmpeg_cmd.c_str(); }
const char *VideoOption::get_ffmpeg_cmd_part() {
    return this->ffmpeg_cmd_part.c_str();
}
const char *VideoOption::get_share_path() { return this->share_path.c_str(); }

uint32_t VideoOption::get_width() { return this->width; }
uint32_t VideoOption::get_height() { return this->height; }
uint16_t VideoOption::get_port() { return this->port; }